import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import Register from '../views/Register.vue'
import Mycenter from '../views/Mycenter.vue'
import Compile from '../views/information/Compile.vue'
import Myattention from '../views/information/Myattention.vue'
import Index from '../views/Column/index.vue'
import Collect from '../views/information/Collect.vue'
import postDetail from '../views/information/postDetail.vue'
// import index1 from '../views/Column/index1.vue'
import Comment from '../views/information/Comment.vue'
import Mycomment from '../views/information/Mycomment.vue'
import Postsearch from '../views/Column/Postsearch.vue'
import Addcolumn from '../views/information/Addcolumn.vue'




Vue.use(VueRouter)

const routes = [

  {
    path: '/postDetail/:id',
    name: 'postDetail',
    component: postDetail
  },
  // 登录页
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  // 注册页
  {
    path: '/register',
    name: 'Register',
    component: Register
  },
  // 主页
  {
    path: '/',
    name: 'Home',
    component: Home,
    //   // 编程式跳转
    //   children: [
    //     { path: '/', component: Home }]
  },
  // 个人中心
  {
    path: '/mycenter',
    name: 'Mycenter',
    component: Mycenter
  },
  //编辑资料
  {
    path: '/compile',
    name: 'Compile',
    component: Compile
  },
  // 我的关注

  {
    path: '/myattention',
    name: 'Myattention',
    component: Myattention
  },
  // 主页
  {
    path: '/index',
    name: 'Index',
    component: Index
  },
  // 我的收藏
  {
    path: '/collect',
    name: 'Collect',
    component: Collect
  },
  // 剩余的跟帖
  {
    path: '/comment/:id', //每当进入页面的时候对进入相应的id的文章跟帖
    name: 'comment',
    component: Comment
  },
  // 我的跟帖
  {
    path: '/mycomment',
    name: 'mycomment',
    component: Mycomment
  },
  // 搜索文章
  {
    path: '/postsearch',
    name: 'postsearch',
    component: Postsearch
  },
  // 栏目操作
  {
    path: '/addcolumn',
    name: 'Addcolumn',
    component: Addcolumn
  },
]

const router = new VueRouter({
  routes,

})
// 添加路由守卫 只管有没有，不管对错
// * to：代表要跳转到哪个页面
// * from：代表从哪个页面跳转过来
// * next：是一个函数，必须要调用才能跳转到下一个页面，不调用则不跳转
router.beforeEach((to, from, next) => {
  if (to.path == '/mycenter') {
    if (localStorage.getItem("token")) {
      // 有token就放行
      return next() //放行
    } else {
      // 不存在，就跳转到首页
      return router.push('/login').catch(err => {

      }) //因为是异步，所以需要返回值
    }
  }
  return next()

})

export default router
